package com.veepeak.server.api.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.google.common.collect.Lists;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.DtcCommandLog;
import com.veepeak.server.service.DtcCommandLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/command-log")
@Api(description = "OBD命令记录接口")
public class CommandLogApi {
    @Autowired
    private DtcCommandLogService dtcCommandLogService;

    @ApiOperation(value = "上传DTC故障代码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "obdId", value = "OBDID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "vehicleId", value = "车辆ID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "faultTime", value = "故障产生时间", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "confirmPids", value = "确认故障PID代码集合,多个代码用逗号隔开(A,B,C)",  paramType = "param", required = true, dataTypeClass = List.class),
            @ApiImplicitParam(name = "pendingPids", value = "等待故障PID代码集合,多个代码用逗号隔开(A,B,C)",  paramType = "param", required = true, dataTypeClass = List.class)
    })
    @PostMapping(value = "/dtc")
    public JSONObject uploadConfirmCode(String userId, String obdId, String vehicleId,String faultTime, String confirmPids,String pendingPids){
        try {
            List<String> confirmPidList = Lists.newArrayList();
            if ( confirmPids.indexOf(",") == -1 ){
                confirmPidList.add(confirmPids);
            }else{
                confirmPidList.addAll(Lists.newArrayList(confirmPids.split(",")));
            }

            List<String> pendingPidList = Lists.newArrayList();
            if ( pendingPids.indexOf(",") == -1 ){
                pendingPidList.add(pendingPids);
            }else{
                pendingPidList.addAll(Lists.newArrayList(pendingPids.split(",")));
            }

            dtcCommandLogService.saveDtcCommands(userId,obdId,vehicleId,faultTime,confirmPidList,pendingPidList );
            return JSONResult.toSuccessJson("success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail : "+e.getMessage());
        }
    }

    @ApiOperation(value = "查询历史故障代码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "obdId", value = "OBDID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "vehicleId", value = "车辆ID", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "startDate", value = "开始时间", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "param", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/history")
    public JSONObject historyDtc(String userId, String obdId, String vehicleId, String startDate, String endDate){
        try {
            Map<String,List<JSONObject>> logs = dtcCommandLogService.history(userId,obdId,vehicleId,startDate,endDate);
            JSONArray result = new JSONArray();
            for(String key : logs.keySet() ){
                JSONObject item = new JSONObject();
                item.put("datetime",key);
                item.put("dtcSize",logs.get(key).size());
                item.put("dtcs",logs.get(key));
                result.add(item);
            }
            return JSONResult.toSuccessJson(result,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

}
